package com.hooper.wallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hooper.wallet.domain.TransactRecord;
import com.hooper.wallet.domain.UserWallet;
import com.hooper.wallet.domain.vo.TransactRecordVo;
import com.hooper.wallet.mapper.TransactRecordMapper;
import com.hooper.wallet.mapper.UserWalletMapper;
import com.hooper.wallet.service.TransactRecordService;
import com.hooper.wallet.util.TransactMapUtil;
import com.hooper.wallet.util.ValidationCheckUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mind
 * @description 针对表【transact_record(交易记录表)】的数据库操作Service实现
 * @createDate 2023-03-07 18:54:52
 */
@Service
public class TransactRecordServiceImpl extends ServiceImpl<TransactRecordMapper, TransactRecord>
        implements TransactRecordService {

    @Resource
    private TransactRecordMapper transactRecordMapper;
    @Resource
    private UserWalletMapper userWalletMapper;

    @Override
    public List<TransactRecordVo> getTransactRecords(Long userId) {
        if (ValidationCheckUtil.isIdNotValidate(userId)) {
            throw new RuntimeException("id not validate!");
        }
        //先获取用户名
        LambdaQueryWrapper<UserWallet> userWrapper = new LambdaQueryWrapper<>();
        //只获取用户名即可，用户名有索引避免回表
        userWrapper.select(UserWallet::getUsername).eq(UserWallet::getId, userId);
        UserWallet userWallet = userWalletMapper.selectOne(userWrapper);
        if (userWallet == null) {
            throw new RuntimeException("user not exist!");
        }
        //由于用户名设置了非空约束，不可能为空，若这里为空则可能是数据库查询异常，或MyBatis-Plus框架异常
        if (!StringUtils.hasText(userWallet.getUsername())) {
            throw new RuntimeException("database error!");
        }
        LambdaUpdateWrapper<TransactRecord> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TransactRecord::getUserId, userId)
                //按创建时间降序排列
                .orderByDesc(TransactRecord::getCreateTime);
        List<TransactRecord> recordList = transactRecordMapper.selectList(wrapper);
        //若无记录，返回空集合
        if (CollectionUtils.isEmpty(recordList)) {
            return Collections.emptyList();
        }
        //映射为VO并返回
        return recordList.stream().map(record -> {
            TransactRecordVo recordVo = new TransactRecordVo();
            recordVo.setUsername(userWallet.getUsername());
            recordVo.setAmount(record.getAmount());
            recordVo.setTransactType(TransactMapUtil.mapToTxt(record.getTransactType()));
            recordVo.setTransactTime(record.getCreateTime());
            return recordVo;
        }).collect(Collectors.toList());
    }
}




